﻿using FreeSql;
using Simple.Contracts.Contracts;
using Simple.Contracts.Enums.Common;
using Simple.Contracts.Models;
using Simple.Contracts.Models.Common;
using Simple.Repository;
using Simple.Repository.SimpleEntitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simple.Services.Services
{
    /// <summary>
    /// 仪表盘
    /// </summary>
    public class DashboardService : BaseService, IDashboardService
    {
        public DashboardService(FreeSqlCloud freeSqlCloud, CurrentAccount currentAccount) : base(freeSqlCloud, currentAccount)
        {

        }

        /// <summary>
        /// 获取公司仪表盘数据
        /// </summary>
        /// <returns></returns>
        public async Task<Result<CompanyDashboardDto>> GetCompanyDashboard()
        {
            var companyDashboardDto = new CompanyDashboardDto();

            companyDashboardDto.ElderlyCount = await _simpleFreeSql.Select<Elderly>()
                .Where(x => x.CompanyId == _currentAccount.CompanyId && x.IsDelete == 0 && x.Status == 1).CountAsync();
            companyDashboardDto.MaleElderlyCount = await _simpleFreeSql.Select<Elderly>()
                .Where(x => x.CompanyId == _currentAccount.CompanyId && x.IsDelete == 0 && x.Status == 1 && x.Gender == (int)GenderEnum.男).CountAsync();
            companyDashboardDto.FemaleElderlyCount = await _simpleFreeSql.Select<Elderly>()
                .Where(x => x.CompanyId == _currentAccount.CompanyId && x.IsDelete == 0 && x.Status == 1 && x.Gender == (int)GenderEnum.女).CountAsync();
            companyDashboardDto.ThisMonthBirthdayElderlyCount = await _simpleFreeSql.Select<Elderly>()
                .Where(x => x.CompanyId == _currentAccount.CompanyId && x.IsDelete == 0 && x.Status == 1 && x.DateOfBirth != null && x.DateOfBirth.Value.Month == DateTime.Now.Month).CountAsync();
            companyDashboardDto.EmployeeCount = await _simpleFreeSql.Select<Employee>()
                .Where(x => x.CompanyId == _currentAccount.CompanyId && x.IsDelete == 0 && x.Status == 1).CountAsync();
         
            return Result<CompanyDashboardDto>.Success(companyDashboardDto);
        }
    }
}
